<template>
  <el-dialog v-model="dialogVisible" title="定制列表" width="600" :before-close="close">
    <div class="CustomHeader">
      <div class="AllFields public">
        <div class="title">所有字段</div>
        <div class="content">
          <div v-for="(item, index) in list" :key="index" class="items_center">
            <vab-icon
              icon="add-circle-fill"
              class="pointer fs20"
              :class="[isAdd(item.id) ? 'c_9a9a9a' : 'c_main']"
              @click="addItem(item)"
            />
            <div class="pl4">{{ item.label }}</div>
          </div>
        </div>
      </div>
      <div class="icon">
        <vab-icon icon="arrow-left-right-line" class="fs20 c_main" />
      </div>
      <div class="ExistingFields public">
        <div class="title">已有字段</div>
        <div class="content">
          <div v-for="(item, index) in ExistingFieldsList" :key="index" class="items_center">
            <vab-icon icon="indeterminate-circle-fill" class="pointer fs20 c_ff4d4f" @click="delItem(index)" />
            <div class="pl4">{{ item.label }}</div>
          </div>
        </div>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="dataSubmit">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
const emit = defineEmits(['update:modelValue', 'dataSubmit'])
const ExistingFieldsList = ref([])
const list = ref([])
const dialogVisible = ref(false)
const close = () => {
  dialogVisible.value = false
}
const addItem = (item) => {
  if (isAdd(item.id)) return
  ExistingFieldsList.value.push(item)
}
const delItem = (index) => {
  ExistingFieldsList.value.splice(index, 1)
}
const isAdd = (id) => {
  return ExistingFieldsList.value.map((item) => item.id).includes(id)
}
const open = (state, ExistingFields, Alllist) => {
  dialogVisible.value = state
  ExistingFieldsList.value = ExistingFields
  list.value = Alllist
}
const dataSubmit = () => {
  dialogVisible.value = false
  ExistingFieldsList.value.sort((a, b) => a.id - b.id)
  emit('dataSubmit', ExistingFieldsList.value)
}
defineExpose({
  open,
})
</script>

<style lang="scss">
.CustomHeader {
  display: flex;
  flex: 1;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  height: calc(100vh - 350px);
  .icon {
    flex: 1;
    text-align: center;
  }
  .title {
    width: calc(100% - 20px);
    padding: 10px;
    color: #1890ff;
    background-color: #e8f4ff;
  }
  .content {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: calc(100% - 20px);
    height: calc(100% - 40px);
    padding: 10px;
    overflow-y: scroll;
    .items_center {
      display: flex;
    }
  }
  .public {
    flex: 4;
    height: 100%;
    border: 1px solid #ecf5ff;
  }
  .pointer {
    cursor: pointer;
  }
  .fs20 {
    font-size: 20px;
  }
  .c_9a9a9a {
    color: #9a9a9a;
  }
  .c_ff4d4f {
    color: #ff4d4f;
  }
  .pl4 {
    padding-left: 14px;
  }
}
</style>
